﻿/**
 * @author 毕伟伦
 * 2012-4-11
 * 
 */
package cn.edu.zjusc.Webservice;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.jws.*;
import javax.jws.soap.SOAPBinding;

import org.jboss.wsf.spi.annotation.WebContext;
import java.io.File;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.*;

import org.w3c.dom.*;

import cn.edu.zjusc.POJO.*;
import cn.edu.zjusc.Security.ProfileInterceptor;
import cn.edu.zjusc.Security.TeacherProfileInterceptor;
import cn.edu.zjusc.Security.UserCave;

@Stateless
@WebService(name = "ConfigService", serviceName = "ConfigService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebContext(contextRoot = "/ZJUSCIX/Service", urlPattern = "/ConfigService")
public class ConfigService {
	String xmlFileName = "config.xml";

	public ConfigInfo readConfig() {

		ConfigInfo config = new ConfigInfo();
		try {
			// DocumentBuilder工厂
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setIgnoringElementContentWhitespace(true);
			DocumentBuilder db = dbf.newDocumentBuilder();

			Document doc = db.parse(new File(xmlFileName));
			doc.normalize();

			Node yearNode = doc.getElementsByTagName("year").item(0);
			config.setYear(yearNode.getFirstChild().getNodeValue());

			Node seasonNode = doc.getElementsByTagName("season").item(0);
			config.setSeason(seasonNode.getFirstChild().getNodeValue());

			Node startTimeNode = doc.getElementsByTagName("startTime").item(0);
			config.setStartTime(startTimeNode.getFirstChild().getNodeValue());

			Node endTimeNode = doc.getElementsByTagName("endTime").item(0);
			config.setEndTime(endTimeNode.getFirstChild().getNodeValue());

			Node registerNode = doc.getElementsByTagName("register").item(0);
			config.setRegister(registerNode.getFirstChild().getNodeValue());

			Node loadNode = doc.getElementsByTagName("load").item(0);
			config.setLoad(loadNode.getFirstChild().getNodeValue());

			Node psNode = doc.getElementsByTagName("pagesize").item(0);
			config.setPageSize(psNode.getFirstChild().getNodeValue());

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return config;

	}

	@Interceptors({ ProfileInterceptor.class, TeacherProfileInterceptor.class })
	public String updateConfig(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "cfg") ConfigInfo cfg) {

		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setIgnoringElementContentWhitespace(true);
			DocumentBuilder db = dbf.newDocumentBuilder();

			Document doc = db.parse(new File(xmlFileName));
			doc.normalize();

			Node yearNode = doc.getElementsByTagName("year").item(0);
			yearNode.getFirstChild().setNodeValue(cfg.getYear());

			Node seasonNode = doc.getElementsByTagName("season").item(0);
			seasonNode.getFirstChild().setNodeValue(cfg.getSeason());

			Node startTimeNode = doc.getElementsByTagName("startTime").item(0);
			startTimeNode.getFirstChild().setNodeValue(cfg.getStartTime());

			Node endTimeNode = doc.getElementsByTagName("endTime").item(0);
			endTimeNode.getFirstChild().setNodeValue(cfg.getEndTime());

			Node registerNode = doc.getElementsByTagName("register").item(0);
			registerNode.getFirstChild().setNodeValue(cfg.getRegister());

			Node loadNode = doc.getElementsByTagName("load").item(0);
			loadNode.getFirstChild().setNodeValue(cfg.getLoad());

			Node psNode = doc.getElementsByTagName("pagesize").item(0);
			psNode.getFirstChild().setNodeValue(cfg.getPageSize());

			doc2XmlFile(doc, xmlFileName);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return "修改配置成功";
	}

	private static void doc2XmlFile(Document document, String filename) {
		try {
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();

			transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");

			DOMSource source = new DOMSource(document);
			StreamResult result = new StreamResult(new File(filename));
			transformer.transform(source, result);

		} catch (TransformerConfigurationException ex) {
			ex.printStackTrace();
		} catch (TransformerException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
}
